package com.play.ballen.me.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.me.entity.TeamInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

class TeamCenterVM : BaseViewModel() {

    private val model = ApiRepository()

    val teamInfo = MutableLiveData<TeamInfo>()
    val actionGetFailure = SingleLiveEvent<String>()

    var page = 1
    private val pageSize = 10

    fun team(percent: String, loading: Boolean = false) {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        map["percent"] = percent
        if (loading) {
            showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        }
        model.team(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                if (loading) {
                    showDialog.setValue(false)
                }
            }
            .subscribe({
                Logger.i("TeamVM.team success")
                teamInfo.value = it
            }, { throwable ->
                actionGetFailure.value = throwable.message
                Logger.i("TeamVM.team error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}